//
//  EvaluateCollectionViewCell.m
//  Project_A Gary
//
//  Created by leilei on 16/3/28.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "EvaluateCollectionViewCell.h"
#import "EvaluateTableViewCell.h"
#import "EvaluateModel.h"
#import "DetailViewController.h"


@interface EvaluateCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableV;
@property (nonatomic, retain)NSMutableArray *dataArr;

@end




@implementation EvaluateCollectionViewCell

-(void)dealloc
{
    [_tableV release];
    [_dataArr release];
    [_navi release];
    [super dealloc];

    
}

// 初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    [self datahandle];
}

// 创建tableView
- (void)createTableView
{
    self.backgroundColor = [UIColor whiteColor];
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS_WIDTH, BOUNDS_HEIGHT - 64 - 49 - 40) style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor whiteColor];
    self.tableV.delegate = self;
    self.tableV.dataSource =self;
    self.tableV.rowHeight = 80;
    [self.contentView addSubview:self.tableV];
    
    // 注册重用池
    [self.tableV registerClass:[EvaluateTableViewCell class] forCellReuseIdentifier:@"EvaluateTableViewCell"];
    [_tableV release];
    
}

// 数据解析
- (void)datahandle
{
    NSString *str = @"http://mrobot.pcauto.com.cn/v2/cms/channels/4?pageNo=1&pageSize=20&v=4.0.0";
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        NSArray *arr = responseObject[@"data"];
        self.dataArr = [EvaluateModel transformWithArray:arr];
        
        [_tableV reloadData];
    } filed:^(NSError *error) {
        
    }];
}

#pragma mark - 点击进入详情页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detail = [[DetailViewController alloc] init];
    
    EvaluateModel *eModel = self.dataArr[indexPath.row];
    
    detail.url = eModel.url;
    [self.navi pushViewController:detail animated:YES];
    
}


// tableView必须写的两个方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EvaluateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EvaluateTableViewCell" forIndexPath:indexPath];
    
    // 通过model类创建个对象
    EvaluateModel *EModel = _dataArr[indexPath.row];
    
    
    cell.timeLab.text = EModel.pubDate;
    cell.titleLab.text = EModel.title;
    
    // 评论数
    if ([EModel.count isEqualToNumber:@(0)]) {
        cell.countLab.text = @"抢沙发";
    } else {
        cell.countLab.text = [NSString stringWithFormat:@"评论数%@", EModel.count];
    }
    
    // 图片
    [cell.leftImgView sd_setImageWithURL:[NSURL URLWithString:EModel.image]];
    
    return cell;
}



@end
